package org.agpsoft.simpledatareceiver;

import org.agpsoft.datareceiverinfrastructure.common.RawData;
import org.agpsoft.datareceiverinfrastructure.datasource.IDataAccessor;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverCloseException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverNULLObjectException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverOpenException;
import org.agpsoft.datareceiverinfrastructure.facade.IDataReceiverFacade;
import org.agpsoft.datareceiverinfrastructure.recorder.IDecodedDataRecorder;
import org.agpsoft.datareceiverinfrastructure.recorder.IRawDataRecorder;
import org.agpsoft.datareceiverinfrastructure.transmitter.IDataTransmitter;

public class SimpleFacade implements IDataReceiverFacade {

	private static final int BUFFER_LEN = 15;
	private IDataAccessor accessor = null;
	private IDataTransmitter transmitter = null;
	private IRawDataRecorder rawRecorder = null;
	private IDecodedDataRecorder decodedRecorder = null;
	
	@Override
	public void requestDataProcess() throws DataReceiverException {
		accessor.requestDataFromSource( BUFFER_LEN );
	}

	@Override
	public void setDataAccessor(IDataAccessor accessor)
			throws DataReceiverNULLObjectException {
		if( accessor == null )
		{
			throw new DataReceiverNULLObjectException();
		}
		this.accessor = accessor;
	}

	@Override
	public void setDataTransmitter(IDataTransmitter transmitter)
			throws DataReceiverNULLObjectException {
		if( transmitter == null )
		{
			throw new DataReceiverNULLObjectException();
		}
		this.transmitter = transmitter;
	}

	@Override
	public void close() throws DataReceiverCloseException {
		accessor.close();
		transmitter.close();
		rawRecorder.close();
		decodedRecorder.close();
	}

	@Override
	public void open(Object context) throws DataReceiverOpenException {
		accessor.open(context);
		transmitter.open(context);
		rawRecorder.open(context);
		decodedRecorder.open(context);
	}

	@Override
	public void setDecodedDataRecorder(IDecodedDataRecorder recorder) 
			throws DataReceiverNULLObjectException {
		if( recorder == null )
		{
			throw new DataReceiverNULLObjectException();
		}
		this.decodedRecorder = recorder;
	}

	@Override
	public void setRawDataRecorder(IRawDataRecorder recorder)
			throws DataReceiverNULLObjectException {
		if( recorder == null )
		{
			throw new DataReceiverNULLObjectException();
		}
		this.rawRecorder = recorder;
	}

	@Override
	public void processData(RawData data) throws DataReceiverException {
		rawRecorder.recordData( data );
		
		SimpleDecodedData decodedData = new SimpleDecodedData();
		
		for( int index = 0 ; index < data.getActiveDataLength() ; ++index )
		{
			decodedData.getDecodeFrame()[index] = (byte) (data.getData()[index] - 1); 
		}
		
		decodedRecorder.recordData( decodedData );
		transmitter.sendData( decodedData );
	}

}
